from time import sleep

from selenium.webdriver.common.by import By

from base.base import Base

url = 'http://192.168.8.222/admin.php'


class Phone4(Base):
    # 获取cookies
    def login(self):
        self.save_cookie('../data/w_warehouse_cookies.json')
        sleep(20)

    # 跳过登录
    def login_admin(self):
        self.set_cookie('../data/w_warehouse_cookies.json')
        self.driver.get(url)
        self.click((By.XPATH, '//*[@id="admin-offcanvas"]/div/ul/li[11]/a'))
        # 点击小程序配置，进入iframe
        self.click((By.XPATH, '//*[@id="power-menu-319"]/li[5]/a'))
        self.go_iframe((By.ID, "ifcontent"))

    def configuration_wechat(self, id, secret, name, describe, versionNumber, version):
        # 小程序ID
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/div[2]/div[1]/input'), id)
        # 小程序密钥
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/div[2]/div[2]/input'), secret)
        # 名称
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/div[2]/div[3]/input'), name)
        # 描述
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/div[2]/div[4]/input'), describe)
        # 拓展插件配置
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/div[2]/div[1]/div'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/div[2]/div[1]/div/div/ul/li[1]'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/div[2]/div[2]/div'))
        # self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/div[2]/div[2]/div/div/ul/li[1]'))
        # 好物推荐组件版本号
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[2]/div[2]/div[3]/input'), versionNumber)
        # 启用直播
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/div[2]/div[4]/div'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/div[2]/div[4]/div/div/ul/li[1]'))
        # 直播组件版本号
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[2]/div[2]/div[3]/input'), version)
        # 保存
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/button'))
        sleep(1)
        print(self.get_text((By.XPATH, '//*[@id="common-prompt"]/p')))
        return self.get_text((By.XPATH, '//*[@id="common-prompt"]/p'))

    def configuration_alipay(self, id, public, private, alipaypublic, name, describe, coding, chatcoding):
        # 支付宝小程序
        self.click((By.XPATH, '/html/body/div[2]/div/ul/li[2]/a'))
        # 配置id
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/div[2]/div[1]/input'), id)
        # 应用公钥
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/div[2]/div[2]/textarea'), public)
        # 应用私钥
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/div[2]/div[3]/textarea'), private)
        # 支付宝公钥
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/div[2]/div[4]/textarea'), alipaypublic)
        # 名称
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/div[2]/div[5]/input'), name)
        # 描述
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/div[2]/div[6]/input'), describe)
        # 在线客服-企业编码
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[2]/div[2]/div[2]/input'), coding)
        # 在线客服-聊天窗编码
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[2]/div[2]/div[2]/input'), chatcoding)
        # 保存
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/button'))
        sleep(1)
        print(self.get_text((By.XPATH, '//*[@id="common-prompt"]/p')))
        return self.get_text((By.XPATH, '//*[@id="common-prompt"]/p'))

    def configuration_baidu(self, id, key, keys, name, describe):
        # 百度小程序
        self.click((By.XPATH, '/html/body/div[2]/div/ul/li[3]/a'))
        # 智能小程序id
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/div[2]/div[1]/input'), id)
        # 智能小程序key
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/div[2]/div[2]/input'), key)
        # 智能小程序密钥
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/div[2]/div[3]/input'), keys)
        # 名字
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/div[2]/div[4]/input'), name)
        # 描述
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/div[2]/div[5]/input'), describe)
        # 保存
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/button'))
        sleep(1)
        print(self.get_text((By.XPATH, '//*[@id="common-prompt"]/p')))
        return self.get_text((By.XPATH, '//*[@id="common-prompt"]/p'))

    # def configuration_headlines(self):
    #     # 头条小程序
    #     self.click((By.XPATH, '/html/body/div[2]/div/ul/li[4]/a'))
    #     #
    #     self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/div[2]/div[1]/input'),id)


if __name__ == '__main__':
    p = Phone4()
    p.login_admin()
    # p.configuration_wechat('12', '23', '34', '45', '56', '67')
    # p.configuration_alipay('1', '2', '3', '4', '5', '6', '7', '8')
    p.configuration_baidu('1', '2', '3', '4', '5')
